from django.shortcuts import render_to_response, get_object_or_404
from accio.ilex.models import Content, Topic, Category

def index(request):
  contents = Content.objects.all()
  categories = Category.objects.all()
  years = Content.objects.dates('date', 'year')
  return render_to_response('ilex/index.html', {'contents' : contents, 'categories' : categories, 'years' : years})

def article(request, slug):
  content = get_object_or_404(Content, slug=slug)
  return render_to_response('ilex/article.html', {'content' : content})

def category_index(request):
  categories = Category.objects.all()
  return render_to_response('ilex/category_index.html', {'categories' : categories})

def category(request, slug):
  category = get_object_or_404(Category, slug=slug)
  topics = Topic.objects.filter(category=category)
  return render_to_response('ilex/category.html', {'category' : category, 'topics' : topics})

def topic(request, slug):
  topic = get_object_or_404(Topic, slug=slug)
  articles = Content.objects.filter(topic=topic)
  return render_to_response('ilex/topic.html', {'topic' : topic, 'articles' : articles})

def archive(request, year):
  articles = Content.objects.filter(date__year=year)
  return render_to_response('ilex/archive.html', {'year' : year, 'articles' : articles})
  
def archive_index(request):
  years = Content.objects.dates('date', 'year')
  return render_to_response('ilex/archive_index.html', {'years' : years})